package MyRevise;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import MyRevise.App;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class WordManagerDialog {
	private Text text;
	private Text text_1;
	
/**
 * @wbp.parser.entryPoint
 */
public void ShowDialog(Shell shell){

		
		final Shell dialog = new Shell(shell, SWT.RESIZE|SWT.DIALOG_TRIM|	SWT.MAX|SWT.MIN);

		FormData fd_dialog = new FormData();
		fd_dialog.left = new FormAttachment(0, 50, 0);
		dialog.setLayoutData(fd_dialog);
		dialog.setText("Word Manager");
		dialog.setSize(304, 189);
		dialog.setLayout(new FormLayout());
		
		text = new Text(dialog, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(0, 218);
		fd_text.left = new FormAttachment(0, 24);
		text.setLayoutData(fd_text);
		
		text_1 = new Text(dialog, SWT.BORDER);
		fd_text.bottom = new FormAttachment(text_1, -26);
		FormData fd_text_1 = new FormData();
		fd_text_1.right = new FormAttachment(text, 0, SWT.RIGHT);
		fd_text_1.top = new FormAttachment(0, 81);
		fd_text_1.left = new FormAttachment(0, 24);
		text_1.setLayoutData(fd_text_1);
		
		
		Button btnSubmit = new Button(dialog, SWT.NONE);
		btnSubmit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// On Submit button add data to DB.
				try {
					insertNewWord(Db.DbEntities.Conn(),text,text_1);			 
		    		
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		FormData fd_btnSubmit = new FormData();
		fd_btnSubmit.top = new FormAttachment(text_1, 6);
		fd_btnSubmit.left = new FormAttachment(text, 0, SWT.LEFT);
		btnSubmit.setLayoutData(fd_btnSubmit);
		btnSubmit.setText("Submit");
		
		Label lblNewLabel = new Label(dialog, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.bottom = new FormAttachment(text, -6);
		fd_lblNewLabel.left = new FormAttachment(text, 0, SWT.LEFT);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("Word");
		
		Label lblNewLabel_1 = new Label(dialog, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(text, 6);
		fd_lblNewLabel_1.left = new FormAttachment(text, 0, SWT.LEFT);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("Translate");
		
			
		
		
		
		

		dialog.open();
		
	}

public static ArrayList<String> insertNewWord(Connection conn, Text wordEb, Text translateEb) throws SQLException{
	String word =	wordEb.getText();
	String translate =	translateEb.getText();
	
	ArrayList<String> result = new ArrayList<String>();
	result.add(word);
	result.add(translate);
	
	Statement stmt = conn.createStatement();
	stmt.executeQuery("INSERT into Vocabulary (word_source, word_target, comments) values('"+word+"','"+translate+"','Comment Section Коментарии');");
	
	return result;
}
}


